package com.example.kajinka.readbook;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private Button button;
    private Button button2;
    private Button button3;
    private Button button4;
    private Button button5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);//加载主窗体
        Log.d("readBook", "onCreateMainActivity");//打印日志
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new ButtonListener());//设置按钮的监听事件
        button2 = (Button) findViewById(R.id.button2);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //意图对象,打开第二个activity
                Intent intent = new Intent();//显示intent
                intent.setClass(MainActivity.this, SecondActivity.class);
                startActivity(intent);
            }
        });

        //打开其他安卓应用(浏览器）
        button3 = (Button) findViewById(R.id.button3);
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse("http://www.baidu.com"));
                startActivity(intent);
            }
        });

        //activity传递数据给下一个activity
        button4 = (Button) findViewById(R.id.button4);
        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String data = "set Data";
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                intent.putExtra("2getDate", data);
                startActivity(intent);
            }
        });

        //启动第二个activity，第二个activity销毁前会返回数据
        button5 = (Button) findViewById(R.id.button5);
        button5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,SecondActivity.class);
                startActivityForResult(intent, 1);
            }
        });
    }

    /**
     * 添加菜单
     *
     * @param menu
     * @return
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);//设置外部menu文件
        return true;
    }

    /**
     * 给菜单添加响应时间
     *
     * @param item
     * @return
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.add_item:
                Toast.makeText(MainActivity.this, "add_item", Toast.LENGTH_LONG).show();
                break;
            case R.id.remove_item:
                Toast.makeText(MainActivity.this, "remove_item", Toast.LENGTH_LONG).show();
                break;
            default:
                break;
        }
        return true;
    }

    class ButtonListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            //隐式intent,需要在AndroidManifest.xml文件中注册的activity中定义myAction
            Intent intent = new Intent("myAction");
            startActivity(intent);
            Toast.makeText(MainActivity.this, "hello world", Toast.LENGTH_LONG).show();//发送helloworld信息到窗口
        }
    }

    //第二个activity销毁后会调用这个方法，接受返回数据
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {//启动活动时传入的请求码
            case 1:
                if (resultCode == RESULT_OK) {//返回数据是传入的处理结果
                    Log.d("返回数据", data.getStringExtra("returnDate"));
                    System.out.print("------------------------------");
                    System.out.print(data.getStringExtra("returnDate"));
                }
                break;
            default:
                break;
        }
    }
}
